package com.dap.utils;

import java.lang.reflect.Field;

/**
 * @author wangmin
 * @discription
 * @created 2020-06-05 14:02
 */
public abstract class ReflectUtil {
    public static Object getBeanTargetIfGlib(Object bean) {
        try {
            Field callback1Field = bean.getClass().getDeclaredField("CGLIB$CALLBACK_1");
            if (null == callback1Field)
                return bean;
            callback1Field.setAccessible(true);
            Object callback1 = callback1Field.get(bean);
            if (null == callback1)
                return bean;

            Field targetField = callback1.getClass().getDeclaredField("target");
            if (null == targetField)
                return bean;
            targetField.setAccessible(true);
            Object target = targetField.get(callback1);
            if (null != target)
                return target;

            return bean;
        } catch (Exception e) {
            return bean;
        }
    }
}
